<?php

/*******************************************************************************
* 
* Poll4All::check.php
* 
* Description: This script checks client's cookies (that's why it must be
* included before any other output) for eventual votes by users.
*
* Author: C4F Team (http://code4fun.org)
*  
*******************************************************************************/

require_once 'includes/configmagik.php';
include_once 'config.php';

if (!isset($txtPath)) $txtPath = $scriptPath."txt/txtdb.ini.php";

// Check if a vote was cast
if ( (isset($_POST['choice'])) && ($_POST['choice']!="") ) {

	// Create and configure new ConfigMagik-Object
	$dbTXT = new ConfigMagik( $txtPath, true, true);
	$dbTXT->SYNCHRONIZE = false;

	// get poll id
	$pollid = $dbTXT->get('pollid', 'MAIN');
	// check if user already voted this poll (pollid)
	if ( !isset($_COOKIE['poll4all']) || $_COOKIE['poll4all']!=$pollid ) {

		// Remembers vote
		$anKey = "an".$_POST['choice']."poll";
		$dbTXT->increment($anKey, 'ANSWERS');
                
		// Sends cookie to clients to mark they already voted
        if ($dbTXT->save()) setcookie( "poll4all", $pollid, time()+60*60*24*360, "/" );
		else print "fatal error: writing file failed.<br />";
	}
}
?>